package pers.qianyu.month_202011.date_20201125;

/**
 * 1143. 最长公共子序列
 * https://leetcode-cn.com/problems/longest-common-subsequence/
 *
 * @author mizzle rain
 * @date 2020-11-25 22:03
 */
public class LongestCommonSubsequence {
    public int longestCommonSubsequence(String text1, String text2) {
        int[][] dp = new int[text1.length() + 1][text2.length() + 1];
        for (int i = 1; i <= text1.length(); i++) {
            for (int k = 1; k <= text2.length(); k++) {
                if (text1.charAt(i - 1) == text2.charAt(k - 1)) {
                    dp[i][k] = dp[i - 1][k - 1] + 1;
                } else {
                    dp[i][k] = Math.max(dp[i - 1][k], dp[i][k - 1]);
                }
            }
        }
        return dp[text1.length()][text2.length()];
    }
}
